//
//  ViewController.swift
//  OT Manager
//
//  Created by Nguyen Van Truong on 12/27/14.
//  Copyright (c) 2014 Nguyen Van Truong. All rights reserved.
//



import UIKit
import CoreData

class ViewController: UIViewController {
    @IBOutlet weak var txtActualOt: UITextField! = nil
    @IBOutlet weak var txtDate: UITextField! = nil
    @IBAction func btnUpdate(sender: AnyObject) {
        if let weekday = getDayOfWeek(txtDate.text) {
            println(weekday)
        } else {
            println("bad input")
        }
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    func getDayOfWeek(today:String)->Int? {
        let formatter  = NSDateFormatter()
        formatter.dateFormat = "yyyy-MM-dd"
        if let todayDate = formatter.dateFromString(today) {
            let myCalendar = NSCalendar(calendarIdentifier: NSGregorianCalendar)
            let myComponents = myCalendar?.component(.WeekdayCalendarUnit, fromDate: todayDate)
            let weekDay = myComponents?.hashValue
            return weekDay
        } else {
            return nil
        }
    }
    override func touchesBegan(touches: NSSet, withEvent event: (UIEvent!)) {
        self.view.endEditing(true)
    }
}

